package zookeeper.zkClientListener;

import java.util.List;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

public class ZkClientWatcher1 {

    /**
     * zookeeper地址
     */
    static final String CONNECT_ADDR = "localhost:2181";
    /**
     * session超时时间
     */
    static final int SESSION_OUTTIME = 5000;//ms


    public static void main(String[] args) throws Exception {
        ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);

        //对父节点添加监听子节点变化。
        zkc.subscribeChildChanges("/super", new IZkChildListener() {

            //只要实现了监听，就不需要重复监听，一次就够了
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println("parentPath: " + parentPath);
                System.out.println("currentChilds: " + currentChilds);
                System.out.println("------------------------");
            }
        });

 /*   Thread.sleep(3000);

    zkc.createPersistent("/super");
    Thread.sleep(1000);

    //不会监听父节点的updata操作
    zkc.writeData("/super","父节点发生变化");
    Thread.sleep(1000);

    zkc.createPersistent("/super" + "/" + "c1", "c1内容");
    Thread.sleep(1000);

    zkc.createPersistent("/super" + "/" + "c2", "c2内容");
    Thread.sleep(1000);

    //不会监听子节点的updata操作
    zkc.writeData("/super"+"/"+"c1","新内容");
    Thread.sleep(1000);

    zkc.delete("/super/c2");
    Thread.sleep(1000);

    //递归删除:这里出发了两次，一次是先删除/super/c1触发一次，然后再删除/super再触发一次
    zkc.deleteRecursive("/super");*/
        Thread.sleep(Integer.MAX_VALUE);
    }
}
